﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
public partial class admin_OrderDetailAdd : SitePage
{
    #region Fields
    busStoreOrder _busOrder = new busStoreOrder();
    busStoreOrderDetail _busOrderDetail = new busStoreOrderDetail();
    public int OrderId
    {

        get
        {
            object o = ViewState["OrderId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["OrderId"] = value; }

    }
    public int OrderDetailId
    {

        get
        {
            object o = ViewState["OrderDetailId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["OrderDetailId"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.OrderId = clsQueryString.GetInt("OrderId");
            this.OrderDetailId = clsQueryString.GetInt("OrderDetailId");
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private void LoadData()
    {
        btnDelete.Visible = false;
        if (this.OrderId > 0 && this.OrderDetailId > 0)
        {
            StoreOrder objOrder = _busOrder.GetByID(this.OrderId);
            if (objOrder == null)
                GoList();
            StoreOrderDetail objDetail = _busOrderDetail.GetByID(this.OrderDetailId);
            if (objDetail != null)
            {
                txtDesignCode.Text = objDetail.DesignCode;
                txtName.Text = objDetail.Name;
                txtQuantity.Text = objDetail.Quantity.ToString();
                if (objDetail.EndDate != DateTime.MinValue)
                    txtEndDate.Text = clsConvert.String(objDetail.EndDate);
                if (objDetail.StartDate != DateTime.MinValue)
                    txtStartDate.Text = clsConvert.String(objDetail.StartDate);

                clsSQL sql = new clsSQL();
                int countFinish = (int)sql.ExecuteScalar("Select COALESCE(sum(Quantity),0) from Entry where OrderDetailId=" + this.OrderDetailId.ToString());
                if (countFinish < 1)
                    btnDelete.Visible = true;
            }

        }

    }
    private void UpdateOrderDetail(StoreOrderDetail objData)
    {

        objData.OrderDetailId = this.OrderDetailId; //''clsQueryString.GetInt("id");
        int result = _busOrderDetail.Update(objData);
        if (result == 1)
            GoList();
        else if (result == -1)//duplicate code
        {
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Mã thiết kế");
        }

        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertOrderDetail(StoreOrderDetail objData)
    {
        int result = _busOrderDetail.Insert(objData);
        if (result > 0)
            GoList();
        else if (result == -1)//duplicate code
        {
            lblErrorMessage.Text = string.Format(Resources.Resource.MSG_CHECK_EXISTS, "Mã thiết kế");
        }
        else
        {
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
        }
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //validate data
            int qty = clsConvert.Int(txtQuantity.Text.Trim());
            if (qty < 0)
            {
                lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Số lượng");
                return;
            }
            DateTime endDate = clsConvert.Date(txtEndDate.Text);
            if (!string.IsNullOrEmpty(txtEndDate.Text))
            {
                if (endDate == DateTime.MinValue)
                {
                    lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Ngày hoàn thành");
                    return;
                }
            }
            DateTime startDate = clsConvert.Date(txtStartDate.Text);
            if (!string.IsNullOrEmpty(txtStartDate.Text))
            {
                if (startDate == DateTime.MinValue)
                {
                    lblErrorMessage.Text = string.Format(Resources.Resource.MSG_VALID, "Ngày bắt đầu");
                    return;
                }
            }
            //get datavalue
            StoreOrderDetail objData = new StoreOrderDetail();
            objData.Name = txtName.Text;
            objData.OrderId = this.OrderId;
            objData.DesignCode = txtDesignCode.Text.Trim();
            objData.Quantity = qty;
            objData.CreatedBy = this.userloginName;
            objData.CreatedDate = clsDateTime.Now;
            objData.ModifiedBy = this.userloginName;
            objData.EndDate = endDate;
            objData.StartDate = startDate;
            objData.ModifiedDate = clsDateTime.Now;
            if (OrderDetailId > 0)
                UpdateOrderDetail(objData);
            else
                InsertOrderDetail(objData);
        }
        else
        {
            vsumTipNew.ShowMessageBox = false;
            vsumTipNew.ShowSummary = true;
        }
    }

    protected void btnCacel_Click(object sender, EventArgs e)
    {
        GoList();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (_busOrderDetail.Delete(this.OrderDetailId))
        {
            GoList();
        }
        //else lblErrorMessage.Text = Resources.Resource.MSG_D;
    }


    private void GoList()
    {
        string paraURL = GetPageParams(FilterFieldType.All);
        Response.Redirect("OrderDetail.aspx?OrderId=" + this.OrderId + "&" + paraURL);
    }

    #endregion


}